package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbOrderItemMapper;
import com.dongdongshop.mapper.TbOrderMapper;
import com.dongdongshop.pojo.TbOrder;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.service.ItemService;
import com.dongdongshop.service.OrderService;
import com.dongdongshop.util.IdWorker;
import com.dongdongshop.vo.Cart;
import com.dongdongshop.vo.OrderVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@DubboService
public class OrderServiceImpl implements OrderService {

    @Autowired
    private TbOrderMapper tbOrderMapper;

    @Autowired
    private TbOrderItemMapper tbOrderItemMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @DubboReference
    private ItemService itemService;

    @Autowired
    private IdWorker idWorker;


    @Override
    public String saveOrder(List<TbOrder> tos, List<TbOrderItem> itemList, String username) {
        long oid = idWorker.nextId();
        String s = String.valueOf(oid);
        for (int i = 0; i < tos.size(); i++) {
            long l = idWorker.nextId();
            TbOrder tbOrder = tos.get(i);
            tbOrder.setOrderId(l);
            tbOrder.setUpdateTime(new Date());
            tbOrder.setUserId(username);
            tbOrder.setReceiverAreaName(tbOrder.getReceiverAreaName());
            System.out.println(tbOrder.getReceiverAreaName());

            tbOrder.setTradeNoOut(s);
            tbOrderMapper.insertSelective(tbOrder);
            for (int a = 0; a < itemList.size(); a++) {
                TbOrderItem tbOrderItem = itemList.get(a);
                if (tbOrder.getSellerId().equals(tbOrderItem.getSellerId())) {
                    long l1 = idWorker.nextId();
                    tbOrderItem.setOrderId(l);
                    tbOrderItem.setId(l1);
                    tbOrderItem.setTradeNoOut(s);
                    tbOrderItemMapper.insertSelective(tbOrderItem);
                    itemService.minusNum(tbOrderItem.getItemId(), tbOrderItem.getNum());
                }
            }
        }
        redisTemplate.boundHashOps("cart").delete(username);
        return s;
    }

    @Override
    public void updateOrderStatus(String out_trade_no, String trade_no) {
        TbOrder order = new TbOrder();
        order.setTradeNoOut(out_trade_no);
        order.setTradeNo(trade_no);
        List<TbOrder> orders = tbOrderMapper.selectByOrder(out_trade_no);
        for (TbOrder tbOrder : orders) {
            Message<String> message = MessageBuilder.withPayload(JSONObject.toJSONString(tbOrder)).build();
            TransactionSendResult result = rocketMQTemplate.sendMessageInTransaction("my_order_topic", message, order);
            System.out.println(result);
        }
    }

    @Override
    public List<OrderVO> findOrderPayList(String username) {

       List<TbOrder> tbOrders = tbOrderMapper.findOrderPayList(username);
       List<OrderVO> voList = new ArrayList<>();
        for (TbOrder order : tbOrders) {
            OrderVO orderVO = new OrderVO();
            orderVO.setTbOrder(order);
            List<TbOrderItem> tbOrderItems =tbOrderItemMapper.selectByOrderId(order.getOrderId());
            orderVO.setItemList(tbOrderItems);
            voList.add(orderVO);
        }
        return voList;

    }

    @Override
    public TbOrderItem selectByOrderIdId(long id) {

        return tbOrderItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateOrderByStatus(Long orderId) {
        tbOrderMapper.updateOrderByStatus(orderId);
    }
}
